'use strict';

module.exports = app => {
  const {
    STRING,
    INTEGER
  } = app.Sequelize;

  const Dynamic = app.model.define('dynamic', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    post_uid: {
      type: INTEGER,
      allowNull: false,
    },
    content: {
      type: STRING,
      allowNull: true,
    },
    img_src: {
      type: STRING,
      allowNull: true,
    },
    video_src: {
      type: STRING,
      allowNull: true,
    },
    state: {
      type: INTEGER,
      allowNull: false,
    },
    post_time: {
      type: INTEGER,
      allowNull: false,
    },
    dynamic_id: {
      type: INTEGER,
      allowNull: true,
    }
  });
  Dynamic.associate = function () {
    app.model.Dynamic.belongsTo(app.model.Info, {
      foreignKey: 'post_uid',
      targetKey: 'user_id',
      as: 'publisher'
    });

    app.model.Dynamic.belongsTo(app.model.Dynamic, {
      foreignKey: 'dynamic_id',
      targetKey: 'id',
      as: 'transmit'
    });

    app.model.Dynamic.hasOne(app.model.Dynamic, {
      foreignKey: 'dynamic_id'
    });


  }

  return Dynamic;
};